﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class ChangeAssiment : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }

            else
            {
                if (!Request["type"].ToString().Equals(String.Empty))
                {
                    if (!Page.IsPostBack)
                    {
                        this.GetData();
                    }
                }
            }

        }

        protected void GetData()
        {
            try
            {
                string type = Request["type"].ToString();

                if (type.Equals("1"))
                {
                    this.TitleLabel.Text = "CAT";
                    this.TitleLabel1.Text = "CAT";
                    this.ActiveLabel.Text = "CAT";
                    this.ActiveLabel1.Text = "CAT";

                    BaseFunction.BindCATDropDownList(this.AllDropDownList, String.Empty, true);
                    BaseFunction.BindActiveCATDropDownList(this.ActiveDropDownList, String.Empty, true);
                }
                else if (type.Equals("2"))
                {
                    this.TitleLabel.Text = "PU";
                    this.TitleLabel1.Text = "PU";
                    this.ActiveLabel.Text = "PU";
                    this.ActiveLabel1.Text = "PU";

                    BaseFunction.BindBDDropDownList(this.AllDropDownList, String.Empty, true);
                    BaseFunction.BindActiveBDDropDownList(this.ActiveDropDownList, String.Empty, true);
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");

            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (this.AllDropDownList.SelectedIndex == 0 || this.ActiveDropDownList.SelectedIndex == 0)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.NotChooseRecord) + "')</script>");
                return;
            }

            try
            {
                string type = Request["type"].ToString();
                DataTable dt = new DataTable();
                BEGDC.PreContract.BLL.IPreContractInfo preContract = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];

                string oldId = this.AllDropDownList.SelectedValue;
                string newId = this.ActiveDropDownList.SelectedValue;


                preContract.ChangeAssignment(type, oldId, newId);

               
                this.Response.Write("<script language='javascript'>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "'); var arr=new Array(); arr[0]=0; window.returnValue=arr;window.close();</script>");
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }


      

    }
}
